﻿//TODO: Test

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// difference of gaussian
    /// </summary>
    public class DifferenceOfGaussian : BaseImageFilter
    {
        protected bool m_bNormalize = false;

        protected GaussianBlur m_GaussianBlur = new GaussianBlur();

        /// <summary>
        /// default constructor
        /// </summary>
        public DifferenceOfGaussian()
        {
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_differenceOfGaussian"></param>
        internal DifferenceOfGaussian(DifferenceOfGaussian _differenceOfGaussian)
        {
        }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="DifferenceOfGaussian"/> is normalize.
        /// </summary>
        /// <value><c>true</c> if normalize; otherwise, <c>false</c>.</value>
        public bool Normalize
        {
            get { return m_bNormalize; }
            set { m_bNormalize = value; }
        }

        /// <summary>
        /// executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmapCloned = (UnsafeBitmap)_bitmap.Clone();
            m_GaussianBlur.Weight = 1;
            m_GaussianBlur.Execute(bitmapCloned);
            
            m_GaussianBlur.Weight = 3;
            m_GaussianBlur.Execute(bitmapCloned);

            m_GaussianBlur.Weight = 7;
            m_GaussianBlur.Execute(bitmapCloned);


            Color color = Color.Transparent;

            new PerPixelFilterCoordinates(delegate(int x, int y, Color _color)
                {
                    Color color1 = bitmapCloned.GetPixel(x, y);
                    int nRDiff = Math.Abs(_color.R - color1.R);
                    int nGDiff = Math.Abs(_color.G - color1.G);
                    int nBDiff = Math.Abs(_color.B - color1.B);

                    if (m_bNormalize)
                    {
                        color = ColorFunctions.NormalizeToColor(Color.FromArgb(nRDiff, nGDiff, nBDiff));
                    }
                    else
                    {
                        color = Color.FromArgb(nRDiff, nGDiff, nBDiff);
                    }
                    return color;
                }).Execute(_bitmap);

            return _bitmap;
        }

        /// <summary>
        /// create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new DifferenceOfGaussian(this);
        }
    }
}
